from EE.models import UserProfile

from django.contrib.auth.models import Group


def getProfile(user):
  """TODO: Docstring
  """

  if not user.is_authenticated():
    return None

  try:
    prof = user.get_profile()
  except UserProfile.DoesNotExist:
    prof = UserProfile(user=user)
    prof.save()
  return prof


def getType(user):
  """TODO: Docstring
  """

  if not user.is_authenticated():
    return ''

  try:
    rtype = user.groups.all().get()
  except Group.DoesNotExist:
    return 'Groep'

  return str(rtype)


def handle_save_user_profile(user, games):
  """TODO: Docstring
  """

  try:
    profile = user.get_profile()
  except UserProfile.DoesNotExist:
    profile = UserProfile(user=user)
    profile.save()

  if getType(user) == 'Groep':
    profile.gamePlay = list(set(games))
  else:
    if getType(user) == 'Leraar':
      profile.gameControl = list(set(games))
  profile.save()
